package au.org.htd.youslide.servlets;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONException;

import au.org.htd.youslide.PMF;
import au.org.htd.youslide.SongProject;
import au.org.htd.youslide.Tag;

@SuppressWarnings("serial")
public class SearchServlet extends HttpServlet {
   private static final Logger log = Logger.getLogger(SearchServlet.class
         .getName());

   public void doGet(HttpServletRequest req, HttpServletResponse resp)
         throws IOException {

      String requestType = req.getParameter("requestType");
      if ("byLetter".equals(requestType)) {
         doByLetter(req, resp);
      } else if ("byNumber".equals(requestType)) {
         doByNumber(req, resp);
      } else if ("byContent".equals(requestType)) {
         doByContent(req, resp);
      } else if ("byId".equals(requestType)) {
         doById(req, resp);
      } else if ("byCreator".equals(requestType)) {
         doByCreator(req, resp);
      } else if ("byTags".equals(requestType)) {
         doByTags(req, resp);
      } else if ("listTags".equals(requestType)) {
         doListTags(req, resp);
      } else {
         log.info("NO REQUEST TYPE!!!");
      }
   }

   private void doByLetter(HttpServletRequest req, HttpServletResponse resp)
         throws IOException {
      String letter = req.getParameter("search_letter").toUpperCase();
      JSONArray ids = SongProject.searchByLetter(letter);
      resp.setCharacterEncoding("UTF-8");
      resp.getWriter().write(ids.toString());
   }

   private void doByNumber(HttpServletRequest req, HttpServletResponse resp)
         throws IOException {
      JSONArray ids = SongProject.searchByNumbers();
      resp.setCharacterEncoding("UTF-8");
      resp.getWriter().write(ids.toString());
   }

   private void doByContent(HttpServletRequest req, HttpServletResponse resp)
         throws IOException {
      String content = req.getParameter("search_string");
      JSONArray ids = SongProject.searchByContent(content);
      resp.setCharacterEncoding("UTF-8");
      resp.getWriter().write(ids.toString());
   }

   private void doByCreator(HttpServletRequest req, HttpServletResponse resp)
         throws IOException {
      String creator = req.getParameter("creator");
      JSONArray ids = SongProject.searchByCreator(creator);
      resp.setCharacterEncoding("UTF-8");
      resp.getWriter().write(ids.toString());
   }

   private void doById(HttpServletRequest req, HttpServletResponse resp)
         throws IOException {
      Long id = Long.parseLong(req.getParameter("id"));
      SongProject song = new SongProject(PMF.getPM()).get(id);
      String letter = song.getFirstLetter().toUpperCase();
      HttpSession session = req.getSession();
      session.setAttribute("id", id);
      session.setAttribute("letter", letter);
      song.closeConnection();
   }

   private void doByTags(HttpServletRequest req, HttpServletResponse resp)
         throws IOException {
      String tags = req.getParameter("tags");
      JSONArray songs = Tag.getSongsByTags(tags);
      resp.setCharacterEncoding("UTF-8");
      resp.getWriter().write(songs.toString());
   }

   private void doListTags(HttpServletRequest req, HttpServletResponse resp)
         throws IOException {
      JSONArray tags;
      try {
         tags = Tag.getTagsArray();
         resp.setCharacterEncoding("UTF-8");
         resp.getWriter().write(tags.toString());
      } catch (JSONException e) {
         resp.getWriter().write("error");
      }
   }
}
